package com.lanyou.esb.cook.proxy.service.impl;

import java.util.Calendar;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

import redis.clients.jedis.exceptions.JedisException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lanyou.esb.cook.proxy.common.ParamFactory;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.ServiceTimeJedisProxy;
import com.lanyou.esb.cook.proxy.service.ParamSetService;
import com.lanyou.esb.cook.proxy.service.ServiceTimeService;

/**
 * 服务器时间
 * 
 * @author Davey.wu
 */
public class ServiceTimeServiceImpl implements ServiceTimeService, Job {

	/** 服务器时间Redis操作代理 **/
	private ServiceTimeJedisProxy serviceTimeJedisProxy;
	/** 参数配置服务接口 **/
	private ParamSetService paramSetService;
	/**执行计划线程池**/
	private Scheduler scheduler;
	/**CRON表达式**/
	private String cron = "0/1 * * * * ?";
	/**名称**/
	private String name;
	/***组*/
	private String group;
	/**工作类类型**/
	private Class jobClass;

	public ServiceTimeServiceImpl() {
		// 每秒执行一次
		this.cron = "0/2 * * * * ?";
		this.name = "ServiceTimeServiceImpl";
		this.group = "ServiceTimeServiceImpl";
		this.jobClass = this.getClass();
	}

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		ServiceTimeServiceImpl service = (ServiceTimeServiceImpl) context
				.getJobDetail().getJobDataMap().get("proxy");
		service.pushToRedis();

	}

	@Override
	public void init() throws Exception {
		// 初始化参数
		//initParamSet();
		System.out.println("定时任务------推送消息到REDIS");
		try {
			scheduler = new StdSchedulerFactory().getScheduler();
			// 创建LzstoneTimeTask的定时任务
			// JobDetail jobDetail = new JobDetailImpl(name, group, jobClass);
			JobDetail jobDetail = JobBuilder.newJob(jobClass)
					.withIdentity(name, group).build();
			JobDataMap jobMap = jobDetail.getJobDataMap();
			jobMap.put("proxy", this);
			// 目标 创建任务计划
			CronTrigger trigger = new CronTriggerImpl(name, group, cron);
			// CronTrigger trigger =
			// TriggerBuilder.newTrigger().withIdentity(name,group).
			scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.scheduleJob(jobDetail, trigger);
			scheduler.start();
		} catch (Exception e) {
			System.out.println("定时任务------推送消息到REDIS，失败！");
			throw e;
		}

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pushToRedis() {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(Calendar.MILLISECOND, 0);
			serviceTimeJedisProxy.setex(serviceTimeJedisProxy
					.getKeyFromObject(calendar.getTimeInMillis()),
					calendar.getTimeInMillis());
		} catch (JedisException e) {

		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void initParamSet() {
		if (ParamFactory.haveParam()) {
			return;
		}
		// 加载参数配置
		paramSetService.init();
	}

	public void setParamSetService(ParamSetService paramSetService) {
		this.paramSetService = paramSetService;
	}

	public ServiceTimeJedisProxy getServiceTimeJedisProxy() {
		return serviceTimeJedisProxy;
	}

	public void setServiceTimeJedisProxy(
			ServiceTimeJedisProxy serviceTimeJedisProxy) {
		this.serviceTimeJedisProxy = serviceTimeJedisProxy;
	}

}
